package util;

import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import po.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


public class MessageUtill {

    public static final String MESSAGE_TEXT = "text";
    public static final String MESSAGE_NEWS = "news";
    public static final String MESSAGE_IMAGE = "image";
    public static final String MESSAGE_VOICE = "voice";
    public static final String MESSAGE_VIDEO = "video";
    public static final String MESSAGE_MUSIC = "music";
    public static final String MESSAGE_LINK = "link";
    public static final String MESSAGE_LOCATION = "location";
    public static final String MESSAGE_EVENT = "event";
    public static final String MESSAGE_SUBSCRIBE = "subscribe";
    public static final String MESSAGE_CLICK = "CLICK";
    public static final String MESSAGE_VIEW = "VIEW";
    public static final String MESSAGE_SCANCODE = "scancode_push";  // 扫描事件

    /**
     * 将xml转换成集合类型
     *
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        Map<String, String> map = new HashMap<String, String>();
        SAXReader saxReader = new SAXReader();
        InputStream ins = request.getInputStream();  //获取文件的输入流
        Document document = saxReader.read(ins);      //sax解析文件
        Element root = document.getRootElement();    //获取xml文件根节点
        List<Element> list = root.elements();        //获取所有节点
        for (Element e : list) {
            map.put(e.getName(), e.getText());
        }
        ins.close();
        return map;
    }


    /**
     * 关注公众号，触发事件对象的组装
     *
     * @param toUserName
     * @param fromUserName
     * @param content
     * @return
     */
    public static String initText(String toUserName, String fromUserName, String content) {
        TextMessage text = new TextMessage();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType(MessageUtill.MESSAGE_TEXT);
        text.setCreateTime(new Date().getTime());
        text.setContent(content);

        return textMessageToXml(text);
    }


    /**
     * 主菜单
     *
     * @return
     */
    public static String menuText() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("欢迎您的关注，请按照菜单提示进行操作:\n\n");
        stringBuffer.append("1、课程介绍\n");
        stringBuffer.append("2、郎思科技介绍\n\n");
        stringBuffer.append("回复?调出此菜单。");
        return stringBuffer.toString();
    }

    /**
     * 菜单选项 1：
     *
     * @return
     */
    public static String firstMenu() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。");
        return stringBuffer.toString();
    }

    /**
     * 菜单选项 2:
     *
     * @return
     */
    public static String secondMenu() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("朗思科技主要是对在校大学生进行全栈培养，为当前互联网公司提供新鲜血液。");
        return stringBuffer.toString();
    }


    /**
     * 图文消息的组装
     *
     * @param toUserName
     * @param fromUserName
     * @return
     */
    public static String initNewsMessage(String toUserName, String fromUserName) {
        String message = null;
        List<News> newsList = new ArrayList<News>();
        NewsMessage newsMessage = new NewsMessage();
        News news = new News();

        //给某一条消息添加数据
        news.setTitle("如何成为一名合格的（Java）程序员");
        news.setDescription("1.基础 2.主流编程工具 3.主流编程框架 4.应用程序服务器 5.云开发");
        news.setPicUrl("http://xupt.ngrok.cc/WeiXin/images/1.jpg");
        news.setUrl("http://www.baidu.com");
        newsList.add(news);

        //给图文消息对象添加数据
        newsMessage.setToUserName(fromUserName);
        newsMessage.setFromUserName(toUserName);
        newsMessage.setCreateTime(new Date().getTime());
        newsMessage.setMsgType(MESSAGE_NEWS);
        newsMessage.setArticleCount(newsList.size());
        newsMessage.setArticles(newsList);

        //将图文消息对象转换成xml类型数据
        message = newsMessageToXml(newsMessage);
        return message;
    }

    /**
     * 图片消息的封装
     *
     * @param toUserName
     * @param fromUserName
     * @return
     */
    public static String initImageMessage(String toUserName, String fromUserName) {
        String message = null;
        Image image = new Image();
        image.setMediaId("S0lMvUkwvs_qQ6H1-5TIkYTHqp9O2aR4DadzJEZbs1qnNnzK8LXEAOvuu1UxFlhD");

        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setFromUserName(toUserName);
        imageMessage.setToUserName(fromUserName);
        imageMessage.setMsgType(MESSAGE_IMAGE);
        imageMessage.setCreateTime(new Date().getTime());
        imageMessage.setImage(image);
        message = imageMessageToXml(imageMessage);
        return message;
    }

    /**
     * 组装音乐消息对象
     * @param toUserName
     * @param fromUserName
     * @return
     */
    public static String initMusicMessage(String toUserName, String fromUserName){
        String message = null;
        Music music = new Music();
        music.setTitle("The Big Bang");
        music.setDescription("生活大爆炸主题曲");
        music.setMusicUrl("http://xupt.ngrok.cc/WeiXin/music/TheBigBang.mp3");
        music.setHQMusicUrl("http://xupt.ngrok.cc/WeiXin/music/TheBigBang.mp3");
        music.setThumbMediaIdTitle("FgaGUm9O6ZZc4X_U3zKO07s6yqDOIFuDC9r8MoppbdqkoRH7slN1zat_dtT5tDkm");

        MusicMessage musicMessage = new MusicMessage();
        musicMessage.setToUserName(fromUserName);
        musicMessage.setFromUserName(toUserName);
        musicMessage.setCreateTime(new Date().getTime());
        musicMessage.setMsgType(MESSAGE_MUSIC);
        musicMessage.setMusic(music);

        message = musicMessageToXml(musicMessage);
        return message;
    }

    /**
     * 将文本消息对象类型转换成xml类型
     *
     * @param textMessage
     * @return
     */
    public static String textMessageToXml(TextMessage textMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", textMessage.getClass());  //传给微信客户端的 xml文件，根节点名称转换成 xml
        return xstream.toXML(textMessage);
    }

    /**
     * 将图文消息对象类型转换成xml类型
     *
     * @param newsMessage
     * @return
     */
    public static String newsMessageToXml(NewsMessage newsMessage) {
        XStream xstream = new XStream();
        xstream.alias("xml", newsMessage.getClass());  //传给微信客户端的 xml文件，根节点名称转换成 xml
        xstream.alias("item", new News().getClass());  //传给微信客户端的 xml文件，包含节点名称转换成 item
        return xstream.toXML(newsMessage);
    }

    /**
     * 将图文消息转换成xml类型
     *
     * @param imageMessage
     * @return
     */
    public static String imageMessageToXml(ImageMessage imageMessage) {
        XStream xStream = new XStream();
        xStream.alias("xml", imageMessage.getClass());
        return xStream.toXML(imageMessage);
    }

    /**
     * 将音乐消息转换成xml类型
      * @param musicMessage
     * @return
     */
    public static String musicMessageToXml(MusicMessage musicMessage){
        XStream xStream = new XStream();
        xStream.alias("xml", musicMessage.getClass());
        return xStream.toXML(musicMessage);
    }


}
